const electron = require('electron');
const { dialog } = require('electron');
const { shell } = require('electron');

const ipc = electron.ipcMain;

const globalShortcut = electron.globalShortcut

// import { dialog }  from 'electron';
// Module to control application life.
const app = electron.app;
const Menu = electron.Menu;
const Tray = electron.Tray;

// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

const path = require('path')
const url = require('url')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 900,
    fullscreenable: true,
    height: 750
  })

  // and load the index.html of the app.
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    // pathname: path.join(__dirname, 'weixin/index.html'),
    protocol: 'file:',
    slashes: true
  }), {
    userAgent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
  })

  //获取机器mac地址
  /*getmac.getMac(function(err, macAddress) {
      if (err) throw err;
      var macAddress = macAddress; //获取mac地址
      //console.log(macAddress);
      mainWindow.loadURL("http://weixin.xajlnkyjy.com/tp323/renshi.php", {
          userAgent: macAddress,
          host: macAddress,
      });
  });*/

  // mainWindow.setTitle(macAddress);
  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
  //注册打开控制台的快捷键
  globalShortcut.register('F7', function() {
    let win = BrowserWindow.getFocusedWindow();
    if (win) {
      // win.webContents.print();
      win.webContents.toggleDevTools();
      /* if (win.webContents.isDevToolsOpened()) {
           win.webContents.closeDevTools();
       } else {
           win.webContents.openDevTools({ detach: false });
       }*/
    }
  });
  //快捷键
  globalShortcut.register('F6', function() {
    let win = BrowserWindow.getFocusedWindow();
    if (!win) return;
    win.reload();
  });
  mainWindow.setMenu(null);
  // mainWindow.maximize();
  mainWindow.on('close1', event => {
    let num = dialog.showMessageBox({
      title: '确认退出系统吗？',
      type: 'warning',
      message: '如果确认退出请点击确认，否则点击取消！',
      buttons: ['确认', '取消'],
    });
    if (num !== 0) event.preventDefault();
  });
  mainWindow.on('closed', function(event) {
    mainWindow = null
  })
}

const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
  app.quit()
} else {
  app.on('second-instance', (event, commandLine, workingDirectory) => {
    // Someone tried to run a second instance, we should focus our window.
    if (mainWindow) {
      if (mainWindow.isMinimized()) mainWindow.restore()
      mainWindow.focus()
    }
  })

  // Create mainWindow, load the rest of the app, etc...
  app.on('ready', () => {
  })
}

app.on('ready', createWindow)

app.on('window-all-closed', function(event) {
  if (process.platform !== 'darwin') {
    app.quit()
  }

})

app.on('activate', function() {
  if (mainWindow === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
